package cz.muni.fi.rum.domainmodel;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author pmikulasek
 */
public class Reflector {

    private Map<Long, ReflectorSession> sessions;

    public Reflector() {
        sessions = new HashMap<Long, ReflectorSession>();
    }

    public void addSession(ReflectorSession session) {
        if (!sessions.containsKey(session.getId())) {
            sessions.put(session.getId(), session);
        }
    }

    public void removeSession(ReflectorSession session) {
        sessions.remove(session.getId());
    }

    public ReflectorSession getReflectorSessionByComponentId(Long componentId) {
        return sessions.get(componentId);
    }

    public ReflectorListener getReflectorListenerByComponentId(Long componentId) {
        //TODO -- tohle by se dalo zrychlit pamatovanim vsech komponent v mape...
        ReflectorListener ret = null;
        for (ReflectorSession session : sessions.values()) {
            ReflectorListener listener = session.getListenerByComponentId(componentId);
            if (listener != null) {
                ret = listener;
                break;
            }
        }
        return ret;
    }

    public ReflectorSession getReflectorSessionByReflectorListener(ReflectorListener listener) {
        if(listener == null){
            throw new NullPointerException("listener");
        }
        ReflectorSession ret = null;
        for (ReflectorSession session : sessions.values()) {
            if (session.containsRelfectorListener(listener)) {
                ret = session;
                break;
            }
        }
        return ret;
    }

    public Collection<ReflectorSession> getAllReflectorSessions() {
        return sessions.values();
    }

    public int size() {
        return sessions.size();
    }
}
